﻿using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.InventoryAudits;
using Management.Systeam.Read.Apis.Application.Command.Useres;
using Management.Systeam.Read.Apis.Application.CommandHandler.Useres;
using MediatR;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.InventoryAudits
{
    public class DeleteAllInventoryCommandHandler : IRequestHandler<DeleteAllInventoryCommand, ApiResult<int>>
    {
        IBaseRepository<InventoryAudit> Users;
   

        public DeleteAllInventoryCommandHandler(IBaseRepository<InventoryAudit> Users)
        {
            this.Users = Users;

        }

        public Task<ApiResult<int>> Handle(DeleteAllInventoryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var data = request.Ids.Split(",").Select(x => int.Parse(x));
                int cout = 0;
                //生成批量逻辑删除
                foreach (var id in data)
                {
                    var depot = Users.GetQuery().Where(x => x.InventoryAudit_Id == id).First();
                    depot.IsDel = true;
                    var Ccout = Users.UpdateAsync(depot).Result;

                    if (Ccout > 0)
                    {
                        cout++;
                    }
                }
                if (cout > 0)
                {
                    result.Message = "删除成功";
                    result.Data = cout;
                    result.Code = ApiEnum.success;
                }
                else
                {
                    result.Message = "未发现";
                    result.Data = cout;
                    result.Code = ApiEnum.error;


                }
            }
            catch (Exception ex)
            {
                //logger.LogError(ex, "批量删除异常");
                result.Message = ex.Message;
                result.Code = ApiEnum.error;
            }
            return Task.FromResult(result);
        }
    }
}
